﻿package code.admin 
{
	import flash.display.*;
	import flash.events.*
	import gemini.utils.*
	public class BackgroundList extends AdminModule
	{
		
		private var list;
		public var dir;
		private var head;
		private var showlist=0;
		public var onselect;
		private var icons;
		public function BackgroundList(mf)
		{
			mainframe=mf;
			head=new adminmod();
			head.txt.text="Background list"
			addChild(head);
			
			init();
			
		}
		public function init()
		{
			
			var ld=new LoadJSONs();
			ld.addFile(mainframe.path+"php/backlist.php",this,"dir");
			ld.onComplete(activate);
			ld.loadFiles();
			
			
		}
		private function activate(e)
		{
			head.btn.addEventListener(MouseEvent.MOUSE_DOWN,downMe);
		}
		override public function openModule()
		{
			showList();
		}
		override public function closeModule()
		{
			hideAll();
		}
		override public function getItem(num)
		{
			return icons[num];
		}
		private function downMe(e)
		{
			if(showlist)
			{
				hideAll();
			}
			else
			{
				showList();
			}
		}
		public function showList()
		{
			//trace(onselect);
			trace("showing background list");
			if(onselect!=null)
			{				
				//onselect(this);
			}
			head.bck.gotoAndStop("on");			
			showlist=1;
			icons=[];
			list=new Sprite();
			list.x=head.width;
			list.y=0;
			addChild(list);
			var cols=6;
			var sy=0;
			for(var n=0;n<dir.length;n++)
			{
				trace("ADMIN BACK : "+dir[n].link);
				icons[n]=new BackItem(this,dir[n]);
				
				icons[n].x=22*(n%cols);
				icons[n].y=sy;
				if((n%cols)==(cols-1))
			   	{
				   sy+=25;
			   	}
				list.addChild(icons[n]);
				
			}
			
			
			
		}
		public function hideAll()
		{
			removeChild(list);
			icons=null;
			list=null;
			head.bck.gotoAndStop(1);			
			showlist=0;
		}
	
		public function setBackground(link)
		{
			
			mainframe.setBackground(link)
		}

	}

}